var express = require('express');   //引入express模块
var app = express();        //创建express的实例

//中间件，解析post提交的数据
var bodyParser = require('body-parser')

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }));

//引入数据库连接池
const db = require("../server/db");
// 引入sql
const sqlMap = require("../server/sqlMap")

app.get('/info',function (req,res) {
	let tableName = req.query.tableName
	// info数据表
	var str1 = ""
	db.query(sqlMap.getTableInfo + tableName, (err,result)=> {
	    if(err){
	        console.log('[SELECT ERROR]:',err.message);
	    }
	    str1 = result
		res.send(str1);  ////服务器响应请求
	 
	});
    
});

// 查找所有表名
app.get('/queryTables',function (req,res) {
	var str2 = ""
	db.query(sqlMap.getTableNames,  (err,result)=> {
	    if(err){
	        console.log('[SELECT ERROR]:',err.message);
	    }
	    str2 = result
		res.send(str2);  ////服务器响应请求
	 
	});
    
});


app.post('/queryTableColumns',function (req,res) {
	let tableName = req.body.tableName
    // 查找指定表的列信息
	var str3 = ""
	db.query(sqlMap.queryTableColumns,[tableName] , (err,result) => {
		debugger
	    if(err){
	        console.log('[SELECT ERROR]:',err.message);
	    }
	    str3 = result
		res.send(str3);  ////服务器响应请求
	 
	});
    
});

app.post('/queryDataByParams',function (req,res) {
	let tableName = req.body.tableName
	let column = req.body.column
	let data = req.body.data
	var queryTableColumns = 'select * from ' + tableName
	if(undefined != column && column != '' && undefined != data && data != ''){
		queryTableColumns += ' where ' + column + ' = "' + data+'"'
	}
	var str3 = ""
	db.query(queryTableColumns,  (err,result)=> {
	    if(err){
	        console.log('[SELECT ERROR]:',err.message);
	    }
	    str3 = result
		res.send(str3);  ////服务器响应请求
	 
	});
    
});

app.listen(3000,function () {    ////监听3000端口
    console.log('Server running at 3000 port');
});